package chapter07._01_ExceptionHanding;

import java.io.EOFException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Scanner;

/**
 * @Version:
 * @Description: 自定义/创建异常类
 * @LastDate: 2020/10/10
 */
public class Test02_CustomException {

    public static void main(String[] args) throws IOException {
        System.out.println(Test02_CustomException.readData(new Scanner(
                Path.of("fundamentals/chapter07/_01_ExceptionHanding/Docs/input"))));
    }

    static String readData(Scanner in) throws CustomFileException {
        int n = 0;
        int len = 10;
        String s = new String();

        while (n < len) {
            if (!in.hasNext()) {
                if (n < len) throw new CustomFileException("There is CustomFileException." +
                        " Actual_length:" + n + ", Expectation:" + len + ".");
            } else {
                ++n;
                s += in.nextInt();
            }
        }

        return s;
    }
}
/*
* 自定义异常类
* 习惯做法是，派生于Exception的某个子类
* 包含两个构造器 分别是无参和有参
* */
class CustomFileException extends EOFException {
    //一个没有详细描述信息的构造器
    public CustomFileException() {}
    //一个有详细描述信息的构造器 通过调用父级消息函数
    public CustomFileException(String msg) {
        super(msg);
//        getMessage();
    }
}